package com.darker.yygh.order.api;

import com.darker.yygh.order.service.PaymentService;
import com.darker.yygh.order.service.WeixinService;
import com.darker.yygh.result.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api("微信支付接口")
@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;

    @Autowired
    PaymentService paymentService;

    //生成微信支付二维码
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable Long orderId){
      Map map=  weixinService.createNative(orderId);
      return Result.ok(map);
    }
    //查询支付状态
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable Long orderId){
        //调用微信接口查询支付状态
        Map<String,String> resultmap=  weixinService.queryPayStatus(orderId);
        if(resultmap==null){
            return Result.fail().message("支付出错");
        }
        //支付成功
        if ("SUCCESS".equals(resultmap.get("trade_state"))) {
            //更新订单状态
            String out_trade_no = resultmap.get("out_trade_no");//订单编码
            paymentService.paySuccess(out_trade_no,resultmap);
            return Result.ok().message("支付成功");
        }

      return Result.ok().message("支付中");
    }
}
